<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <div id="app">
        <!-- 全选框 -->
        <label for="allChecked">全选</label>
        <input type="checkbox" id="allChecked" @change="allSelectFn" :checked="isAllSelect">
        <!-- 购物车列表 -->
        <ul>
            <li v-for="(item,index) in dataArr">
                <input type="checkbox" :checked="item.checked" @change="changeSelectFn(item)">
                商品名称: <span>{{item.name}}</span> |
                商品单价: <span>{{item.price}}</span> |
                数量: <input type="number" min="1" v-model.number="item.num"> |
                小计: <span>¥ {{item.num * item.price}}</span>
            </li>
        </ul>
        <span>总价:¥ {{total}} </span>
    </div>

    <script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
    <script>
        let vm = new Vue({
            el: "#app",
            data: {
                // 是否全选
                isAllSelect: false,
                dataArr: [{
                        id: 1,
                        name: "iPhone14",
                        price: "5999",
                        num: 10,
                        checked: false
                    },
                    {
                        id: 2,
                        name: "iPhone14 Pro",
                        price: "7999",
                        num: 1,
                        checked: false
                    },
                    {
                        id: 3,
                        name: "iPhone 14 Pro Max",
                        price: "8999",
                        num: 1,
                        checked: false
                    }
                ]
            },
            computed: {
                // 总价
                total(){
                    let sum=0;
                    this.dataArr.forEach(item =>{
                        if(item.checked){
                            sum+=item.num * item.price;
                        }
                    });
                    return sum;
                }
            },
            methods: {
                //    全选/全不选
                allSelectFn(){
                    // 改变全选框的状态
                    this.isAllSelect=!this.isAllSelect;
                    // 全选/全不选操作
                    this.dataArr.forEach(item =>
                    item.checked=this.isAllSelect);
                },
                // 单个复选框操作
                changeSelectFn($item){
                    // 改变复选框的状态
                    $item.checked=!$item.checked;
                    // 判断全选状态
                    // 如果当前复选框存在一个或者多个没有选中，你们全选状态就为false
                    let res=this.dataArr.some(item=>!item.checked);
                    if(res){
                        // 有未选中的
                        this.isAllSelect=false
                    }else{
                        // 找不到未选中的
                        this.isAllSelect=true;
                    }
                    // console.log(res,"true找到没有选中的 ");
                }
            }
        })
    </script>
</body>

</html>